<?php

class ExcelRead
{
    public $excelReader;
    public $file;

    public static $realColumns = 0;

    //遇到最大的空格数 取消读取
    const MAX_EMPTY = 3;


    public function __construct($file)
    {
        $this->setFile($file);
        require_once dirname(__DIR__) . '/lib/PHPExcel/Classes/PHPExcel/IOFactory.php';
    }

    public function setFile($file)
    {
        if (file_exists($file)) {
            $this->file = $file;
            return;
        }
        throw new Exception("文件不存在");
    }

    public function readExcel()
    {
        try {
            $data        = [];
            $emptyColumn = 0;
            $this->setExcelReader();
            //加载excel
//            $reader = $this->excelReader->load($this->file);
            //都可以
            $reader = PHPExcel_IOFactory::load($this->file);
            //获取要读取的sheet (0开始)
            $sheet = $reader->getSheet(0);
            //取得列的总数 (字母格式)
            $maxColumns = $sheet->getHighestColumn();
            //将字母格式的最大列数转换为数字
            $maxColumns = PHPExcel_Cell::columnIndexFromString($maxColumns);
            //获取最大行数 (数字格式)
            $maxRows = $sheet->getHighestRow();
            //循环获取表格信息
            //循环规则 先行后列
            //从第2行开始 因为第1行为表头信息
            for ($iR = 1; $iR <= $maxRows; $iR++) {
                if (self::$realColumns) {
                    $maxColumns = self::$realColumns;
                }
                $row = [];
                //内循环获取对应的单元格信息
                //使用函数 : getCellByColumnAndRow(列索引 从0开始, 行索引 从0开始)
                for ($iC = 0; $iC < $maxColumns; $iC++) {
                    //读取值
                    $column = $sheet->getCellByColumnAndRow($iC, $iR)->getCalculatedValue();
                    //读取值 ，若是有函数会显示函数
//                    $column = $sheet->getCellByColumnAndRow($iC, $iR)->getValue();
                    //和第一个类似
//                    $column = $sheet->getCellByColumnAndRow($iC, $iR)->getFormattedValue();
                    $row[]  = $column;
                    if ($iR == 1) {//重新定义宽度
                        self::$realColumns += 1;
                        if ($column === "" || $column == null || $column == 'null') {
                            $emptyColumn += 1;
                        }
                        if ($emptyColumn >= self::MAX_EMPTY) {
                            self::$realColumns -= self::MAX_EMPTY;
                            $row               = array_slice($row, 0, self::$realColumns);
                            break;
                        }
                    }
                }
                $data[] = $row;
            }
            return [true, $data];
        } catch (Exception $e) {
            return [false, $e->getMessage()];
        }
    }

    public function setExcelReader()
    {
        $this->excelReader = new PHPExcel_Reader_Excel2007();//2007及以上版本
        if ($this->excelReader->canRead($this->file)) {
            return true;
        }
        $this->excelReader = new PHPExcel_Reader_Excel5();
        if ($this->excelReader->canRead($this->file)) {
            return true;
        }
        $this->excelReader = new PHPExcel_Reader_Excel2003XML();
        if ($this->excelReader->canRead($this->file)) {
            return true;
        }
        throw new Exception("not read file");
    }
}